###################comon standout area start###################
LOWEST_JDK_VERSION=12
WARNING="[\e[1;36mWARN\e[0m]  - "
ERROR="[\e[1;35mERROR\e[0m] - "
NOTE="[\e[1;34mNOTE\e[0m]  - "

mainPath="${HOME}/temp/yapai/"
#projectName="shuiliandong"
#branchName="dev"
environ=$1
project=$2
install=""
upload=""
profile="ypcli"
helpflag=""
property=""
keep_branch="false"
###################comon standout area end###################
while getopts ":e:p:i:u:P:h:k:D:" opt
do
    case $opt in
        e)
        environ=$OPTARG
        echo "您输入的环境配置:$environ"
        ;;
        p)
        project=$OPTARG
        echo "您输入的项目配置:$project"
        ;;
        i)
        install=$OPTARG
        echo "您输入的打包开关配置:$install"
        ;;
        u)
        upload=$OPTARG
        echo "您输入的上传开关配置:$upload"
        ;;
        P)
        profile=$OPTARG
        echo "您输入的打包环境配置:$profile"
        ;;
        D)
        property=$OPTARG
        echo "Your system property is : ${property}"
        ;;
        h)
        helpflag=$OPTARG
        echo "您输入的帮助手册:$helpflag"
        ;;
        k)
        keep_branch=$OPTARG
        echo "是否已项目分支为准(default: false):$keep_branch"
        ;;
        ?)
        echo "未知参数,你可以这样使用 : sh shuiliandong.sh -e 111 -p sld -i install -u upload -P profile -Dyplicdis"
        exit 1;;
    esac
done
load_environment(){
    currentHome=$( dirname "$(readlink -f  ${BASH_SOURCE[0]})" )
    currentHome=$(dirname "${currentHome}")
    # 读取服务器
    IP=(`cat ${currentHome}/enviroment/${environ} | jq -r '.ip'`)
    EPORT=(`cat ${currentHome}/enviroment/${environ} | jq -r '.port'`)
    if [[ "${EPORT}" -gt 0 ]] 2>/dev/null
    then
     echo "you set port=${EPORT}"
    else
     EPORT=22
    fi
    # 读取用户名
    USER_NAME=(`cat ${currentHome}/enviroment/${environ} | jq -r '.user_name'`)
    # 读取密码
    PASSWORD=(`cat ${currentHome}/enviroment/${environ} | jq -r '.password'`)
    # 读取存放位置
    SAVE_PATH=(`cat ${currentHome}/enviroment/${environ} | jq -r '.save_path'`)

}
check_java_version(){
  SU_JAVA_VERSION_CMD="sudo java -version 2>&1 | sed -n '1p' | awk '{print \$3}' | awk -F '\"' '{print \$2}'"
  JAVA_VERSION_CMD="java -version 2>&1 | sed -n '1p' | awk '{print \$3}' | awk -F '\"' '{print \$2}'"
  SU_JAVA_VERSION=`sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no -p ${EPORT} $USER_NAME@$IP "${SU_JAVA_VERSION_CMD}"`
  JAVA_VERSION=`sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no -p ${EPORT} $USER_NAME@$IP "${JAVA_VERSION_CMD}"`
  SU_MAIN_VERSION=${SU_JAVA_VERSION%%.*}
  MAIN_VERSION=${JAVA_VERSION%%.*}

  if [[ ${MAIN_VERSION} -lt ${LOWEST_JDK_VERSION} ]]
  then
    echo -e "${ERROR} The JDK version installed by ordinary users is too low , suggest ${LOWEST_JDK_VERSION}"
  fi

  if [[ ${SU_MAIN_VERSION} -lt ${LOWEST_JDK_VERSION} ]]
  then
    echo -e "${WARNING} The JDK version installed by root users is too low , suggest ${LOWEST_JDK_VERSION}"
  fi
}

check_server_port(){
  dependency_ports=(8848 5432 3306 6379 9092 8700)
  for PORT in ${dependency_ports[@]}
  do
    pid=`sshpass -p $PASSWORD ssh $USER_NAME@$IP "netstat -ntl | grep :${PORT}" | awk -F "[ :]+" '{print $4}'`
    if [[ -z $pid ]]
    then
      echo -e "$ERROR Port $PORT is not open"
    else
      echo -e "$NOTE Port $PORT is opened"
    fi
  done
}

check_permission_fd(){
  fds_cmd="ls -lR ${SAVE_PATH} | grep -v '^$' | grep -E '^[-d]' | awk '{print \$3\"@\"\$4\"@\"\$NF}'"
  fds=`sshpass -p $PASSWORD ssh $USER_NAME@$IP "${fds_cmd}"`
  server_user=`sshpass -p $PASSWORD ssh $USER_NAME@$IP "whoami"`
  for filed in ${fds[@]}
  do
    if [[ ! "$filed" =~ ^${server_user}@${server_user}.*$ ]]
    then
      echo -e "$ERROR named ${filed##*@} File permissions have been modified, please check..."
    fi
  done
}
###################main###################
load_environment
check_java_version
check_server_port
check_permission_fd
